package com.slkj.shilixiaoyuanapp.ui.main.message;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.BaseLazyFragment;
import com.slkj.shilixiaoyuanapp.entity.BxDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.MessageListEntity;
import com.slkj.shilixiaoyuanapp.entity.RepairDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolLeaveEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolMeetingDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolOfficialSealDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolOutWorkDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolPurchaseDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolWplyDetailEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.net.service.ToolService;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.OutWork.OutWorkDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.OutWork.OutWorkPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.OutWork.OutWorkWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.PunchClock.KqSsActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.Purchase.PurchaseDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.Purchase.PurchasePassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.goods.GoodsDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.goods.GoodsPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.goods.GoodsWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.leave.LeaveDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.leave.LeavePassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.leave.LeaveWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.meeting.MeetingDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.meeting.MeetingPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.meeting.MeetingWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.notice.NoticeDetailActivity;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.notice.NoticeDetailActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.officialSeal.SealDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.officialSeal.SealPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.officialSeal.SealWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.reimbursement.ReimbursementDspActivityActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.reimbursement.ReimbursementPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.reimbursement.ReimbursementWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.repair.RepairDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.repair.RepairPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.repair.RepairWtgActivity__JumpCenter;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.List;

import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.slkj.shilixiaoyuanapp.entity.Enum.AdministrationToolType.*;

/**
 * Created  on 2018-07-18
 *
 * @author 唐开阔
 * @describe
 */
public class MessageNoticeAdapter extends BaseMultiItemQuickAdapter<MessageListEntity, BaseViewHolder> {
    BaseLazyFragment lazyFragment;
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MessageNoticeAdapter(List<MessageListEntity> data, BaseLazyFragment lazyFragment) {
        super(data);
         this.lazyFragment = lazyFragment;
        addItemType(0, R.layout.item_message_type1);
        addItemType(1, R.layout.item_message_type2);
        addItemType(2, R.layout.item_message_type3);
//        addItemType(4, R.layout.item_message_type4);

    }

    @Override
    protected void convert(BaseViewHolder helper, MessageListEntity item) {
        switch (helper.getItemViewType()) {
            case 0:
                break;
            case 1:
                Glide.with(lazyFragment).load(item.getImage()).into((ImageView) helper.getView(R.id.img_head));
                helper.setText(R.id.tv_name,item.getBody())
                .setText(R.id.tv_time,item.getIntime())
                .itemView.setOnClickListener(v-> onClick(item));
                break;
            case 2:
                break;
        }
    }



    private void onClick(MessageListEntity item){
        if (getByValue(item.getType()) == null){
            return;
        }
        ToolService toolService = HttpHeper.get().toolService();
        int id = item.getTypeId();
        switch (getByValue(item.getType())){
            case SS:
                KqSsActivity__JumpCenter.builder(lazyFragment.getContext())
                        .setId(item.getTypeId())
                        .setFromType(2)
                        .create().go();
                break;
            case WPLY:
                toolService.getWplyDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolWplyDetailEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolWplyDetailEntity data) {
                                switch (data.getState()) {
                                    case 0:
                                        GoodsDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        GoodsWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        GoodsPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case CG:
                toolService.getPurchaseDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolPurchaseDetailEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolPurchaseDetailEntity data) {
                                switch (data.getState()) {
                                    case 0:
                                        PurchaseDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        PurchasePassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        PurchasePassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case BX:
                toolService.getBaoxiaoDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<List<BxDetailEntity>>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(List<BxDetailEntity> list) {
                                BxDetailEntity data = list.get(0);
                                switch (data.getState()) {
                                    case 0:
                                        ReimbursementDspActivityActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        ReimbursementWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        ReimbursementPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });

                break;
            case CC:
                toolService.getCcDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolOutWorkDetailEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolOutWorkDetailEntity data) {
                                switch (data.getState()) {
                                    case 0:
                                        OutWorkDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        OutWorkWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        OutWorkPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case QJ:
                toolService.getLeaveDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolLeaveEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolLeaveEntity data) {
                                switch (data.getState()) {
                                    case 0:
                                        LeaveDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        LeaveWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        LeavePassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case YT:
                toolService.getMettingDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolMeetingDetailEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolMeetingDetailEntity data) {
                                switch (data.getState()) {
                                    case 0:
                                        MeetingDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        MeetingWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        MeetingPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case GZ:
                toolService.getOfficialSealDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolOfficialSealDetailEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolOfficialSealDetailEntity data) {
                                switch (data.getState()) {
                                    case 0:
                                        SealDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        SealWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        SealPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case BXIU:
                toolService.getRepairDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<List<RepairDetailEntity>>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(List<RepairDetailEntity> list) {
                                RepairDetailEntity data = list.get(0);
                                switch (data.getState()) {
                                    case 0:
                                        RepairDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        RepairWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        RepairPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case YTTC:

                break;
            case XSQJ:
                break;
            case TZXQ:
                NoticeDetailActivity__JumpCenter.builder(getContext())
                        .setId(id)
                        .setTitle("通知详情")
                        .setType(NoticeDetailActivity.TYPE_MESSAGE)
                        .create().go();
                break;
        }
    }

    private Context getContext() {
        return lazyFragment.getContext();
    }

    public <T> ObservableTransformer<T, T> getThread() {
        return upstream -> upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

    private  <T> LifecycleTransformer<T> bindToLifecycle() {
        return lazyFragment.bindToLifecycle();
    }
}

